package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.CrHcManagement;
import com.arong.swing.db.entity.PagingInfo;
import com.arong.swing.db.entity.PagingResult;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class CrHcManagementSDAO  extends BaseDAO {
	public CrHcManagementSDAO(){
		this(DB_CHECKRECORD);
	}
	public CrHcManagementSDAO(String dbFile){
		super(dbFile);
	}
	public CrHcManagementSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"CR_HC_MANAGEMENT("+
						 " ID  VARCHAR2(32) PRIMARY KEY,"+
						 " HCJL_ID VARCHAR2(32),"+
						 " CHECKEVENT_ID VARCHAR2(32),"+
						 " TYPE CHAR(1) NOT NULL,"+
						 " ZJHM  VARCHAR2(32) ,"+
						 " PCKSSJ DATE,"+
						 " XCSPYPZL BLOB,"+
						 " CZFS  VARCHAR2(10),"+
						 " DESC_CORR VARCHAR2(1000)"+
						 ")";
		
	}
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(CrHcManagement entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getId())) {
				entity.setId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 修改
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int update(CrHcManagement entity) throws SysException,AppException {
		int i=0;
		try {
			i=jdbcUtils.update(entity, "id");
		} catch (SQLException e) {
			throw new SysException("update error",e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(CrHcManagement entity) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM CR_HC_MANAGEMENT WHERE ID='"
					    +entity.getId()
		    		    +"'"
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}

	/**
	 * 分页查询
	 * @param sql
	 * @param pagingInfo
	 * @param entity
	 * @return 
	 * @throws SysException, AppException
	 */
	public  PagingResult<CrHcManagement> queryList(PagingInfo pagingInfo, CrHcManagement entity)
			throws SysException,AppException {
		PagingResult<CrHcManagement> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrHcManagement.class,
				pagingInfo, getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList分页查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<CrHcManagement> queryList(CrHcManagement entity) throws SysException,AppException {
	    List<CrHcManagement> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), CrHcManagement.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  CrHcManagement queryBean(CrHcManagement entity) throws SysException,AppException {
		CrHcManagement res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), CrHcManagement.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}

	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(CrHcManagement crHcManagement) {
		List<Object> qryList = new ArrayList<Object>();
		if (crHcManagement.getId() != null) {
			qryList.add(crHcManagement.getId());
		}
		if (crHcManagement.getHcjlId() != null) {
			qryList.add(crHcManagement.getHcjlId());
		}
		if (crHcManagement.getCheckeventId() != null) {
			qryList.add(crHcManagement.getCheckeventId());
		}
		if (crHcManagement.getType() != null) {
			qryList.add(crHcManagement.getType());
		}
		if (crHcManagement.getZjhm() != null) {
			qryList.add(crHcManagement.getZjhm());
		}
		if (crHcManagement.getPckssj() != null) {
			qryList.add(crHcManagement.getPckssj());
		}
		if (crHcManagement.getXcspypzl() != null) {
			qryList.add(crHcManagement.getXcspypzl());
		}
		if (crHcManagement.getCzfs() != null) {
			qryList.add(crHcManagement.getCzfs());
		}
		if (crHcManagement.getDescCorr() != null) {
			qryList.add(crHcManagement.getDescCorr());
		}
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(CrHcManagement crHcManagement) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.ID,a.HCJL_ID,a.CHECKEVENT_ID,a.TYPE,a.ZJHM,a.PCKSSJ,a.XCSPYPZL,a.CZFS,a.DESC_CORR");
		sql.append(" from CR_HC_MANAGEMENT a where 1=1");
		if (crHcManagement.getId() != null) {
			sql.append(" and ID=?");
		}
		if (crHcManagement.getHcjlId() != null) {
			sql.append(" and HCJL_ID=?");
		}
		if (crHcManagement.getCheckeventId() != null) {
			sql.append(" and CHECKEVENT_ID=?");
		}
		if (crHcManagement.getType() != null) {
			sql.append(" and TYPE=?");
		}
		if (crHcManagement.getZjhm() != null) {
			sql.append(" and ZJHM=?");
		}
		if (crHcManagement.getPckssj() != null) {
			sql.append(" and PCKSSJ=?");
		}
		if (crHcManagement.getXcspypzl() != null) {
			sql.append(" and XCSPYPZL=?");
		}
		if (crHcManagement.getCzfs() != null) {
			sql.append(" and CZFS=?");
		}
		if (crHcManagement.getDescCorr() != null) {
			sql.append(" and DESC_CORR=?");
		}
		return sql.toString();
	}
}

